package com.zhuhao.utils.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author haozhu7
 * @Date 2023/10/26 14:33
 */
public class Demo {

    @Test
    public void test04() {
        // 构造用户数据
        List<User> userList = new ArrayList<>();
        userList.add(new User("小红", 18, 168));
        userList.add(new User("小白", 17, 165));
        userList.add(new User("小蓝", 18, 169));

        // 构造部门数据
        List<Department> deptList = new ArrayList<>();
        deptList.add(new Department("java开发部", "DEV001", "南京", "总部"));
        deptList.add(new Department("测试部", "TEST001", "上海", "研发中心"));
        deptList.add(new Department("财务", "ECONOMY001", "南京", "总部"));

        // 构造产品数据
        List<Goods> goodsList = new ArrayList<>();
        goodsList.add(new Goods("小面包", "速食", "2023-07-21", "3天", "成都", "上海"));
        goodsList.add(new Goods("旺旺仙贝", "膨化食品", "2023-07-21", "3天", "仙贝中心", "山寨仙贝厂"));
        goodsList.add(new Goods("领克03", "汽车", "2023-07-21", "永久", "领克工厂", "领克副厂"));

        // 构造各个sheet页相关信息
        List<SheetInfoBean> sheetInfoList = new LinkedList<>();
        sheetInfoList.add(new SheetInfoBean("用户信息", User.class, userList));
        sheetInfoList.add(new SheetInfoBean("部门信息", Department.class, deptList));
        sheetInfoList.add(new SheetInfoBean("产品信息", Goods.class, goodsList));

        // 导出文件
        File file = new File("E:/多sheet导出测试.xlsx");
        try (ExcelWriter excelWriter = EasyExcel.write(file).build()) {
            WriteSheet writeSheet;
            for (SheetInfoBean bean : sheetInfoList) {
                // 构建sheet对象
                writeSheet = EasyExcel.writerSheet(bean.getSheetName()).head(bean.getHeadClass()).build();
                // 写出sheet数据
                excelWriter.write(bean.getDataList(), writeSheet);
            }
            // 关流
            excelWriter.finish();
        } catch (Exception e) {
            // do something you want
        }
    }
}
